using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.AddressableAssets.Build;

public class AAMgr : Singleton<AAMgr>
{
    public AddressableAssetGroup GetOrCreateGroup(string groupName)
    {
        AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
        AddressableAssetGroup group = settings.FindGroup(groupName);
        if (group == null)
        {
            group = AddressableAssetSettingsDefaultObject.Settings.CreateGroup(groupName, false, false, false, null);
        }

        return group;
    }

    public void EnableEntryPrefab(GameObject go, string groupName)
    {
        var group = GetOrCreateGroup(groupName);
        //获取Prefab的GUID
        string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(go));
        //获取addressable setting entry
        AddressableAssetEntry group_entry = AddressableAssetSettingsDefaultObject.Settings.FindAssetEntry(guid);
        //如果没有entry则创建
        if (group_entry == null)
        {
            group_entry = AddressableAssetSettingsDefaultObject.Settings.CreateOrMoveEntry(guid, group);
        }else{
            //获取默认的addressable settings
            AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
            settings.MoveEntry(group_entry, group);
        }
    }

    public void DeleteEntryPrefab(GameObject go)
    {
        string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(go));
        AddressableAssetEntry group_entry = AddressableAssetSettingsDefaultObject.Settings.FindAssetEntry(guid);
        if (group_entry != null)
        {
            AddressableAssetSettingsDefaultObject.Settings.RemoveAssetEntry(group_entry.guid);
        }
    }
}
